package com.xlh.support.attendance.controller;

import com.xlh.common.Result;
import com.xlh.dto.conclude.BusiFullMarkDTO;
import com.xlh.security.UserInfoHolder;
import com.xlh.support.attendance.po.AttendanceBeginPO;
import com.xlh.support.attendance.po.AttendanceListQueryPO;
import com.xlh.support.attendance.po.AttendanceRecordPO;
import com.xlh.support.attendance.po.SettingMarkPO;
import com.xlh.support.attendance.service.AttendanceService;
import com.xlh.support.attendance.vo.AttendanceResultVO;
import com.xlh.support.attendance.vo.AttendanceSettingMarkVO;
import com.xlh.support.attendance.vo.AttendanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@Api(tags = "AttendanceController", description = "学生考勤")
@RestController
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private AttendanceService attendanceService;

    @ApiOperation("查询考勤名单")
    @PostMapping("/query")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<AttendanceVO>> listStudents(@RequestBody @Valid AttendanceListQueryPO attendanceListQueryPO) {
        List<AttendanceVO> attendanceVOs = attendanceService.listAttendanceStudents(attendanceListQueryPO, false);
        return Result.success(attendanceVOs);
    }

    @ApiOperation("增减旷课次数")
    @PostMapping
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result recordAbsent(@RequestBody @Valid AttendanceRecordPO attendanceRecordPO) {
        BusiFullMarkDTO busiFullMarkDTO = attendanceService.recordAbsent(attendanceRecordPO);
        return Result.success(busiFullMarkDTO);
    }

    @ApiOperation("开始学生签到")
    @PostMapping("/begin-record")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result beginRecord(@RequestBody @Valid AttendanceBeginPO attendanceBeginPO) {
        attendanceService.beginRecord(userInfoHolder.getUserId(), attendanceBeginPO.getClassIdList());
        return Result.success();
    }

    @ApiOperation("学生签到")
    @GetMapping("/record")
    @PreAuthorize("@permissionValidator.isStudent()")
    public Result record() {
        attendanceService.record(userInfoHolder.getUserId());
        return Result.success();
    }

    @ApiOperation("关闭签到")
    @PostMapping("/end-record/{courseId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<AttendanceResultVO> endRecord(@PathVariable("courseId") Long courseId,
                                                @RequestBody @Valid AttendanceBeginPO attendanceBeginPO) {
        AttendanceResultVO attendanceResultVO = attendanceService.endRecord(userInfoHolder.getUserId(), courseId,
                attendanceBeginPO.getClassIdList());
        return Result.success(attendanceResultVO);
    }

    @ApiOperation("查询当前签到班级")
    @GetMapping("/current-record")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<Long>> currentRecord() {
        List<Long> currentAttendanceClassIds = attendanceService.currentAttendance(userInfoHolder.getUserId());
        return Result.success(currentAttendanceClassIds);
    }


    @ApiOperation("设置满分项")
    @PostMapping("/settingfullmark")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result settingFullMark(@RequestBody @Valid SettingMarkPO settingMarkPO) {
        attendanceService.settingFullMark(settingMarkPO);
        return Result.success();
    }


    @ApiOperation("获取满分项设置")
    @GetMapping("/getFullMarkSetting")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result getFullMarkSetting( @RequestParam("classId") Long classId,
                                      @RequestParam("courseId") Long courseId ) {
        AttendanceSettingMarkVO fullMarkSetting = attendanceService.getFullMarkSetting(classId, courseId);
        return Result.success(fullMarkSetting);
    }



}
